/**
 * 
 */
package com.lemote.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * @author wuhui
 *
 * 下午02:02:09
 */
@Entity
@Table(name="smt_group")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Group extends IdEntity {

	/**
	 * 用户组名
	 */
	private String groupName;
	
	/**
	 * 添加时间
	 */
	private Date createTime;
	
	/**
	 * 组成员
	 */
	private List<User> users;
	
	/**
	 * 组角色
	 */
	private List<Role> roles;
	
	/**
	 * 隶属组
	 */
	private Group parent;
	
	/**
	 * 下级组
	 */
	private List<Group> children;
	
	public String status;

	
	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REFRESH,CascadeType.REMOVE }, fetch = FetchType.LAZY, targetEntity = Group.class)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Group> getChildren() {
		return children;
	}

	public Date getCreateTime() {
		return createTime;
	}


	public String getGroupName() {
		return groupName;
	}

	@ManyToOne
	@JoinColumn(name="pid")
	public Group getParent() {
		return parent;
	}

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE},fetch = FetchType.LAZY)   
	@JoinTable(name="smt_group_role", joinColumns={@JoinColumn(name="groupid")}, inverseJoinColumns={@JoinColumn(name="roleid")})
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoles() {
		return roles;
	}

	@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE },  fetch = FetchType.LAZY, mappedBy="groups")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<User> getUsers() {
		return users;
	}

	public void setChildren(List<Group> children) {
		this.children = children;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}


	public void setParent(Group parent) {
		this.parent = parent;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
}
